package com.heima.tess4j;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;

import java.io.File;

/**
 * @author 晓风残月Lx
 * @date 2023/8/6 23:42
 */
public class Application {
    /**
     * 识别图片的文字
     *
     * @param args
     */
    public static void main(String[] args) {
        try {
            //创建Tesseract对象
            ITesseract tesseract = new Tesseract();

            //设置字体库路径
            tesseract.setDatapath("C:\\Users\\16253\\Desktop\\images\\tessdata");

            //设置语言
            tesseract.setLanguage("chi_sim");

            File file = new File("C:\\Users\\16253\\Desktop\\images\\image-20210524161243572.png");

            String result = tesseract.doOCR(file);

            //替换回车和tal键  使结果为一行
            result = result.replaceAll("\\r|\\n", "-").replaceAll(" ", "");

            System.out.println("识别的结果为：" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
